/* 
 * File:   TMessage.cpp
 * Author: JM
 * 
 * Created on 13 maj 2010, 17:50
 */

#include <boost/lexical_cast.hpp>
#include "TMessage.h"
#include <sstream>

using boost::lexical_cast;
using boost::bad_lexical_cast;

TMessage::TMessage() {
}

TMessage::~TMessage() {
}

void TMessage::addAtom(std::string aValue)
{
    boost::shared_ptr<TAtomBase> newAtom(new TAtomString(aValue));
    if(!firstAtom)
        firstAtom=newAtom;

    if(lastAtom)
        lastAtom->setNext(newAtom);
    
    lastAtom=newAtom;
}

void TMessage::addAtom(int aValue)
{
    boost::shared_ptr<TAtomBase> newAtom(new TAtomNumeric(aValue));
    if(!firstAtom)
        firstAtom=newAtom;

    if(lastAtom)
        lastAtom->setNext(newAtom);

    lastAtom=newAtom;
}

std::string TMessage::toString() const
{
    boost::shared_ptr<TAtomBase> atom = firstAtom;
    std::stringstream ss;
    std::string res;

    ss<<"(";
    while(atom)
    {
        if(atom->getType()==ENumber)
        {
            //std::cout<<static_cast<TAtomNumeric*>(atom.get())->value<<std::endl;
            ss<<(static_cast<TAtomNumeric*>(atom.get())->getValue())<<",";
        }
        else
        {//string
            ss<<"\""<<static_cast<TAtomString*>(atom.get())->getValue()<<"\",";
        }

        atom=atom->getNext();
    }

    res = ss.str();
    return res.substr(0,res.length()-1)+')';
}

